import { Component, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { ActionSheetConfig, ActionSheetComponent, SkinType, ActionSheetService } from 'ngx-weui';
import { ApiService } from 'src/app/core/api';
import { StorageService } from 'src/app/core/sotrage/storage.service';
import { I18nService } from '../../core/i18n/i18n.service';

@Component({
	templateUrl: './index.component.html',
	styleUrls: ['./index.less'],
})
export class WebsiteRedeployComponent implements OnInit {
	@ViewChild('auto', { static: true }) autoAS: ActionSheetComponent;
	constructor(
		private srv: ActionSheetService,
		private i: I18nService,
		private router: Router,
		private fb: FormBuilder,
		private api: ApiService,
		private sto: StorageService) {
		this.i.get(['MSG.register-code', 'MSG.register-user']).subscribe(res => {
			this.msgData = res;
		});
	}
	lang = this.sto.get('lang')?this.sto.get('lang'):'zh';
	menus: any[] = [{ text: '自主类型', value: 'Private' }, { text: '系统分配', value: 'System' }];
	config: ActionSheetConfig = {
		title: '',
	  } as ActionSheetConfig;
	checkbox: any[] = [];
	dialogShow = false;
	isShowUrl = true;
	form: FormGroup;
	msgData: any;
	selUrlType = this.menus[0];
	pageData = {
		pageNo: 1,
		type: "H5Game"      // H5Game H5游戏,OnlineGame 网络游戏
	}
	img: any;
	imgShow: boolean = false;
	ngOnInit() {
		this.configForm();
	}
	disabled = false;
	gameList = [];
	configForm() {
		this.form = this.fb.group(
			{
				siteName: ['', [Validators.required]],// 站点名称
				urlType: ['', [Validators.required]],// url类型 Private 自主类型,System 系统分配
				url: [''],
				logo: ['', [Validators.required]],
				copyright: ['', [Validators.required]],// 站点信息
				games: ['']// 游戏编号 (gameListOfAgency.do中获取)
			}
		);
		this.api.post('游戏列表', this.pageData).then(res => {
			this.gameList = res['data']['list'];
			for (let i = 0; i < this.gameList.length; i++) {
				this.gameList[i]['checkd'] = false;
			}
		});
		this.api.post('站点配置详情', {}).then(res => {
			let userDel = res['data']['agencyExtendUrlInfo'];
			if(userDel.copyright){
				this.disabled = true;
			}
			this.form.patchValue(userDel);
			for (let i = 0; i < this.menus.length; i++) {
				const text = this.menus[i].value;
				console.log(text,userDel['urlType'])
				if(text == userDel['urlType']){
					this.selUrlType = this.menus[i];
				}
			}
			this.selGame = res['data']['agencyExtendUrlInfo']['gameIds'];
			this.setSelGame();
			this.form.patchValue({ urlType: this.selUrlType['value'] });
		});
	}
	setSelGame() {
		this.gameList.forEach(item => {
			for (let k = 0; k < this.selGame.length; k++) {
				const id = this.selGame[k];
				if (item['id'] == id) {
					item.checkd = true;
				}
			}
		});
	}
	onNext(){
		if(this.disabled){
			this.isShowUrl = false;
		}else{
			const data = {...this.form.value};
			if(!data.games){
				data.games = [];
			}
			this.api.post('创建站点配置', data).then((res:any)=>{
				this.isShowUrl = false;
			});
		}
	}
	onSave(f) {
		// const data = this.form.value;
		const data = {games: this.selGame};
		this.api.post('编辑站点配置', data).then(res => {
			this.sto.set('user', res['data']);
			this.router.navigate(['/']);
		});
	}
	onPotSave(f) {
		this.api.post('编辑站点信息',{ games:this.selGame}).then(res => {
			this.router.navigate(['/']);
		});
	}
	onShow(type: SkinType, backdrop: boolean = true) {
		if(this.disabled){
			return;
		}
		this.config.skin = type;
		this.config.backdrop = backdrop;
		this.srv.show(this.menus, this.config).subscribe((res: any) => {
			this.selUrlType = res;
			this.form.patchValue({ urlType: res.value });
		});
	}
	changeGames(t) {
		this.pageData.type = t;
		this.api.post('游戏列表', this.pageData).then(res => {
			this.gameList = res['data']['list'];
			this.setSelGame();
		});
	}
	closeGameBody() {
		this.dialogShow = false;
	}
	selGame = [];
	selectGame(item) {
		item.checkd = !item.checkd;
		if(item.checkd){
			this.selGame.push(item.id);
		}else{
			for (let i = 0; i < this.selGame.length; i++) {
				if(this.selGame[i] == item.id){
					this.selGame.splice(i,1);
				}				
			}
		}
		this.form.patchValue({ games: this.selGame})
	}
}

